<template>
  <div class="useravatar-conatainer">
    <el-main class="main">
      <el-card class="userArvart-card">
        <div slot="header" class="clearfix">
          <span style="font-size: 14px">更换头像</span>
        </div>
        <!-- :auto-upload="false" -->
        <el-upload
          class="avatar-uploader"
          ref="upload"
          :action="uploadUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          accept=".jpg,.jpeg,.png,.gif,.webp,.svg"
          :http-request="httpRequest"
          :auto-upload="false"
        >
          <div class="avatar-wrapper">
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <img v-else src="../../assets/img/avatar.b0a47c8b.jpg" />
          </div>

          <div class="bottom">
            <el-button
              slot="trigger"
              size="medium"
              type="primary"
              icon="el-icon-plus"
              >选择图片</el-button
            >
            <el-button
              style="margin-left: 10px"
              size="medium"
              type="success"
              icon="el-icon-upload"
              @click="submitUpload"
              >上传头像</el-button
            >
          </div>
        </el-upload>
      </el-card>
    </el-main>
  </div>
</template>

<script>
import { updateAvatarApi } from "@/api/user";
export default {
  name: "userAvatar",
  data() {
    return {
      uploadUrl: process.env.VUE_APP_URL + "/my/update/avatar",
      imageUrl: "",
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      //res：上传成功之后返回前端数据
      //file:上传成功之后服务器返回前端数据
      console.log("handleAvatarSuccess", res);
      console.log("handleAvatarSuccess", file);
      // this.imageUrl = res.data.url;
    },
    beforeAvatarUpload(file) {
      console.log("beforeAvatarUpload", file);
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isLt2M;
    },
    // 上传头像
    submitUpload() {
      this.$refs.upload.submit();
    },
    // 上传的方式
    // 有Bug，后面修复
    httpRequest(obj) {
      console.log("httpRequest---obj", obj);

      let reader = new FileReader();
      reader.readAsDataURL(obj.file);
      reader.onload = async (result) => {
        console.log(result);
        console.log(reader);
        this.imageUrl = reader.result;
        const res = await updateAvatarApi({ avatar: reader.result });
        console.log("httpRequest---res", res);
        if (res.data.code === 0) {
          this.$message.success("更新图片成功！");
        }
      };
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  // margin-right: 20px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: calc(100% - 20px);
  height: 345px;
  font-size: 12px !important;
  .el-form-item {
    font-size: 12px !important;
  }
}
::v-deep .el-input__inner {
  width: 400px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar-wrapper {
  width: 350px;
  height: 350px;
  .avatar {
    width: 350px;
    height: 350px;
    // display: block;
  }
}
.bottom {
  display: flex;
  margin-top: 10px;
}
</style>